package screens
{
	import events.NavigationEvent;
	
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.utils.HAlign;
	import starling.utils.VAlign;

	/**
	 *游戏结束 
	 * @author Nestor
	 * 
	 */	
	public class GameOver extends Sprite
	{
		private var bgImage:Image;
		private var mainBtn:Button;
		private var playAgainBtn:Button;
		private var overText:TextField;
		private var scoreText:TextField;
		
		public function GameOver()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		
		private function addToStage(e:Event):void
		{
			this.bgImage = new Image(Assets.getTexture("BgWelcome"));
			this.addChild(bgImage);
			bgImage.width = stage.stageWidth;
			bgImage.height = stage.stageHeight;
			
			this.overText = new TextField(300,100,"GAME OVER",Assets.getFont().name,52,0xffffff);
			overText.vAlign = VAlign.TOP;
			overText.hAlign = HAlign.CENTER;
			overText.x = stage.stageWidth * 0.5 - overText.width*0.5;
			overText.y = 100;
			this.addChild(overText);
			
			this.scoreText = new TextField(300,50,"YOU SCORE:",Assets.getFont().name,32,0xffffff);
			scoreText.width = scoreText.textBounds.width+5;
			scoreText.x = stage.stageWidth * 0.5 - scoreText.width*0.5;
			scoreText.y = 200;
			this.addChild(scoreText);
			
			this.mainBtn = new Button(Assets.getAtlas().getTexture("gameOver_mainButton"));
			this.addChild(mainBtn);
			mainBtn.x= stage.stageWidth*0.25 - mainBtn.width*0.5 +50;
			mainBtn.y = stage.stageHeight * 0.6;
			
			this.playAgainBtn = new Button(Assets.getAtlas().getTexture("gameOver_playAgainButton"));
			this.addChild(playAgainBtn);
			playAgainBtn.x = stage.stageWidth*0.75 - mainBtn.width*0.5 -50;
			playAgainBtn.y = stage.stageHeight * 0.6;
			
			this.mainBtn.addEventListener(Event.TRIGGERED,mainCKHandler);
			this.playAgainBtn.addEventListener(Event.TRIGGERED,playAgainBtnCKHandler);
			disposeTemporarily();
		}
		
		private function playAgainBtnCKHandler(e:Event):void
		{
			dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN,{id:"play"},true));
		}
		
		private function mainCKHandler(e:Event):void
		{
			dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN,{id:"main"},true));
			
		}
		
		public function initialize(obj:Object):void{
			this.visible = true;
			scoreText.text ="YOUR SCORE: "+int(obj.score);
			
			GameSound.getInstance().playLose();
		}
		
		public function disposeTemporarily():void{
			this.visible = false;
		}
	}
}